#include "userinfo.h"
#include "ui_userinfo.h"

UserInfo::UserInfo(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::UserInfo)
{
    ui->setupUi(this);
    ui->pushButton_ok->setEnabled(false);
    connect(ui->pushButton,SIGNAL(clicked()),this,SLOT(ChoosePic()));
    connect(ui->pushButton_ok,SIGNAL(clicked()),this,SLOT(SendUserInfo()));
    //connect(ui->pushButton_cancel,SIGNAL(clicked()),this,SLOT());
}

UserInfo::UserInfo(QTcpSocket *s, QString name,QWidget *parent):
    QDialog(parent),
    ui(new Ui::UserInfo),
    socket(s),
    username(name)
{

}
void UserInfo::setInfo(QTcpSocket *s, QString u)
{
    this->socket = s;
    this->username = u;
}
UserInfo::~UserInfo()
{

    delete ui;
}

void UserInfo::DealAnswer()
{
    QMessageBox::information(this,"通知","FFFF");
    QByteArray array = socket->readAll();
    char * ptr = array.data();
    Answer * answer = (Answer *)ptr;
    if(answer->answer == true)
    {
        QMessageBox::information(this,"通知","登录成功");
    }else
    {
        QMessageBox::information(this,"通知","登录失败");
    }
}


void UserInfo::SendUserInfo()
{
    UserInfoMsg msg;
    QString nickname = ui->lineEdit_nickname->text();
    QString sig = ui->lineEdit_sig->text();
    qDebug()<<readPath;
    sprintf(msg.path,readPath.toStdString().c_str());
    sprintf(msg.username,username.toStdString().c_str());
    sprintf(msg.nickname,nickname.toStdString().c_str());
    sprintf(msg.signature,sig.toStdString().c_str());



    msg.cmd = CMD_REG_USERINFO;


    int res = socket->write((const char *)&msg,MSG_LENGTH);
    //connect(socket,SIGNAL(readyRead()),this,SLOT(DealAnswer()));
    if(res < 0)
    {
        QMessageBox::critical(this,"error","错误");
    }
    //connect(socket,SIGNAL(readyRead()),this,SLOT(DealAnswer()));
    this->close();


}


void UserInfo::ChoosePic()
{
    QString filter = "所有文件(*.*);;C++(*.cpp *.h);;视频(*.mp4 *.avi)";
    QString path = QFileDialog::getOpenFileName(this,"打开","D:/",filter);
    if(path != "")
    {
        QString tmp = path;
        tmp.remove(0,43);
        ;
        tmp.remove(0,5);
        readPath = "/new/prefix1" + tmp;
        //QMessageBox::information(this,"ddd",readPath);
        // 显示在组建上
        //ui->textBrowserOpen->append(readPath);
    }
    else if(path=="" && readPath=="")
        QMessageBox::warning(this,"提示","请选择要打开的文件！");
    ui->pushButton_ok->setEnabled(true);

}
